package hxx.topic;

import com.rabbitmq.client.*;
import hxx.utils.MQConnectionUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Author 小坏
 * @Date 2020/6/29 17:54
 * @Version 1.0
 * @program 短信消费者
 */


public class CustomerSmsRouting {


    //队列名称
    private static final String SMS_QUEUE = "sms_queue_routing";

    //交换机名称
    private static final String DESTINATION_NAME = "my_routing_estimation";

    public static void main(String[] args) throws IOException, TimeoutException {
        System.out.println("短信消费者:");
        //1、创建mq链接
        Connection connection = MQConnectionUtils.newConnection();
        //2、创建通道
        Channel channel = connection.createChannel();

        //3、消费声明队列
        channel.queueDeclare(SMS_QUEUE, false, false, false, null);


        //4、消费者绑定队列   第三个参数不能给null
        channel.queueBind(SMS_QUEUE, DESTINATION_NAME, "email");
        channel.queueBind(SMS_QUEUE, DESTINATION_NAME, "sms");

        //如果想收到短信既能收到邮件、多谢一个绑定即可
        //channel.queueBind(EMAIL_QUEUE, DESTINATION_NAME, "sms");

        //5、消费监听消息
        DefaultConsumer defaultconsumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                super.handleDelivery(consumerTag, envelope, properties, body);
                String msg = new String(body, "UTF-8");
                System.out.println("短信消费者获取生产消息" + msg);
            }
        };

        //自动签收
        channel.basicConsume(SMS_QUEUE, true, defaultconsumer);

    }
}
